// 记忆函数
const _ = require('lodash')

function getArea() {
    return Math.PI*r*r
}

let getAreaWithMemory = _.memoize(getArea) 
console.log(getAreaWithMemory(4))
//从缓存中取数据
console.log(getAreaWithMemory(4))
console.log(getAreaWithMemory(4))

//模拟memoize的实现
function memoize (f) {
    //把f存储
    let cache = {}
    return function () {
        let key = JSON.stringify(arguments)
        cache[key] = cache[key] || f.apply(f, arguments) 
        return cache[arg_str]
    }
}